package cn.lingyangwl.agile.model.module.auth;

import cn.lingyangwl.framework.tool.core.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 用户登录记录
 * @author shenguangyang
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginRecord implements Serializable {
    private static final long serialVersionUID = 1L;
    private long loginTime;

    /**
     * 登录id, 每次登录都一个标识
     */
    private String loginId;
    /**
     * token有效期, 单位是s
     */
    private long accessTokenValidity;

    public boolean isExpired() {
        return System.currentTimeMillis() > (loginTime + accessTokenValidity * 1000L);
    }

    public String genCacheValue() {
        return loginTime + "_" + loginId + "_" + accessTokenValidity;
    }

    public static LoginRecord parseCacheValue(String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        String[] split = value.split("_");
        return new LoginRecord(Long.parseLong(split[0]), split[1], Long.parseLong(split[2]));
    }

}
